package com.hy.dao.impl;

import com.hy.bean.ReceiveApplyDetail;
import com.hy.dao.ReceiveApplyDetailDao;
import com.hy.utils.DBManager;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.lang3.StringUtils;

import java.sql.Connection;
import java.util.List;
import java.util.Map;

public class ReceiveApplyDetailDaoImpl implements ReceiveApplyDetailDao {
    private QueryRunner qr = new QueryRunner();
    @Override
    public Boolean insert(ReceiveApplyDetail receiveApplyDetail) throws Exception {
        Connection conn = DBManager.getConnection();
        String sql = "insert into receive_apply_detail values(seq_receive_apply_detail.nextval,?,?,?,?,?)";
        int count = qr.update(conn, sql, receiveApplyDetail.getLysqid(),
                receiveApplyDetail.getWpid(),
                receiveApplyDetail.getSqnum(),
                receiveApplyDetail.getDjnum(),
                receiveApplyDetail.getDjje()
        );
        DbUtils.close(conn);
        return count>0;
    }

    @Override
    public List<Map<String, Object>> select(Integer id) throws Exception {
        Connection conn = DBManager.getConnection();
        String sql = "select rad.*,g.goodsname " +
                " from receive_apply_detail rad " +
                " inner join goods g on rad.wpid = g.id " +
                " where rad.lysqid = ?";
        List<Map<String, Object>> maps = qr.query(conn, sql, new MapListHandler(), id);
        DbUtils.close(conn);
        return maps;
    }

    @Override
    public Boolean update(ReceiveApplyDetail receiveApplyDetail) throws Exception {
        Connection conn = DBManager.getConnection();
        String sql = "update receive_apply_detail set wpid=?, sqnum=?,djnum=?,djje=? where lymxid = ?";
        return qr.update(conn,sql,receiveApplyDetail.getWpid(),receiveApplyDetail.getSqnum(),receiveApplyDetail.getDjnum(),receiveApplyDetail.getDjje(),receiveApplyDetail.getLymxid())>0;
    }

    @Override
    public Boolean delete(String lymxid) throws Exception {
        Connection conn = DBManager.getConnection();
        String sql = "delete from receive_apply_detail where lymxid = ? ";
        return qr.update(conn,sql,lymxid)>0;
    }

    @Override
    public List<Map<String, Object>> select(String status) throws Exception {
        Connection conn = DBManager.getConnection();
        String sql = "select rad.*,g.goodsname " +
                " from receive_apply_detail rad " +
                " inner join goods g on rad.wpid = g.id " ;
        if (!StringUtils.isBlank(status)) {
            sql = sql + " where rad.status like ?";
            return qr.query(conn,sql,new MapListHandler(),status);
        }

        return qr.query(conn,sql,new MapListHandler());
    }
}
